using System;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Widgetsphere.Autos.Websitev2.Objects;
using Widgetsphere.Autos.API.FacetedNavigation;
using HP.Datastore.Common;

namespace Widgetsphere.Autos.Websitev2.UserControls
{
    public partial class ListingGrid : System.Web.UI.UserControl
    {
        #region Class Members

        private ListingResults _autoItemCollection = null;

        #endregion

        #region Page Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.PagingControl1.ObjectSingular = "Auto";
            this.PagingControl1.ObjectPlural = "Autos";

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        #endregion

        #region OnItemDataBound

        protected void OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                var cell = (ListingCell)e.Item.FindControl("ListingCell1");
                cell.Populate((ListingItem)e.Item.DataItem, _autoItemCollection);
            }
        }

        #endregion

        #region Methods

        public void Populate(ListingResults results)
        {
            _autoItemCollection = results;

            this.PagingControl1.ItemCount = results.TotalRecordCount;
            if (results.RecordList.Count == 0)
            {
                pnlNoResults.Visible = true;
                pnlMain.Visible = false;
                return;
            }

            grdList.DataSource = results.RecordList;
            grdList.DataBind();

            #region Sorting
            lblSort.Text = "Sort by: ";
            var sfList = ConfigurationManager.AppSettings["SortFields"];
            if (sfList != null)
            {
                var l = sfList.Split(',');
                foreach (var s in l)
                {
                    var dimension = RepositoryHelper.MasterResults.DimensionList.FirstOrDefault(x => x.Name == s);
                    if (dimension != null)
                    {
                        var query = new ListingQuery(this.Request.Url.PathAndQuery);
                        query.FieldSorts.Clear();
                        var sort = ListingQuery.GetSortByName(s);
                        if (sort != null) query.FieldSorts.Add(sort);
                        lblSort.Text += "<a href=\"" + query.ToString() + "\">" + s + "</a>";
                    }
                }
            }
            #endregion

            #region Setup header
            var headerText = string.Empty; // "Used Cars in Metro/State"
            var refinementList = new List<IRefinementItem>();
            RepositoryHelper.MasterResults.DimensionList.ForEach(x => refinementList.AddRange(x.RefinementList));
            refinementList = refinementList.Where(x => results.Query.DimensionValueList.Contains(x.DVIdx)).ToList();

            var conditionDimension = RepositoryHelper.MasterResults.AllDimensions.ConditionDimension.RefinementList.FirstOrDefault(x => results.Query.DimensionValueList.Contains(x.DVIdx));
            if (conditionDimension != null)
                headerText += conditionDimension.FieldValue + " ";
            headerText += "Cars ";

            var metroDimension = RepositoryHelper.MasterResults.AllDimensions.MetroDimension.RefinementList.FirstOrDefault(x => results.Query.DimensionValueList.Contains(x.DVIdx));
            if (metroDimension != null)
            {
                headerText += "in " + metroDimension.FieldValue + " ";
            }
            else if (RepositoryHelper.MasterResults.AllDimensions.StateDimension != null)
            {
                var refinement = RepositoryHelper.MasterResults.AllDimensions.StateDimension.RefinementList.FirstOrDefault(x => results.Query.DimensionValueList.Contains(x.DVIdx));
                if (refinement != null)
                    headerText += "in " + refinement.FieldValue + " ";
            }

            #endregion

            var t = headerText.Trim();
            if (t == "Cars")
                t = "New and used cars to choose from!";

            //lblHeader.Text = results.TotalRecordCount.ToString("###,###,###,##0") + " " + t;

        }

        #endregion

    }
}